$(function () {
    var menu_datagrid = $("#menu_datagrid");
    var menu_dialog = $("#menu_dialog");
    var menu_form = $("#menu_form");
    menu_datagrid.datagrid({
        url: '/menu/query.do',
        fit: true,
        fitColumns: true,
        columns: [[
            {field: 'name', title: '名称', width: 100,},
            {field: 'url', title: 'URL', width: 100,},
            {
                field: 'parent', title: '父级菜单', width: 100, formatter: function (value) {
                return value ? value.name : '';
            }
            },
        ]],
        singleSelect: true,
        pagination: true,
        toolbar: "#menu_btns",
    });
    menu_dialog.dialog({
        buttons: "#save_btns",
        closed: true,
        onClose: function () {
            menu_form.form('clear');
        }
    });
    var methodObj = {
        add: function () {
            menu_dialog.dialog('setTitle', '新增菜单');
            menu_dialog.dialog('open');
        },

        edit: function () {
            var row = menu_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', "请选中一条数据", 'warning');
                return;
            }
            row['parent.id'] = row.parent.id;
            menu_form.form('load', row
            );
            menu_dialog.dialog('setTitle', '编辑菜单');
            menu_dialog.dialog('open');
        },
        changeState: function () {
            var row = menu_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', "请选中一条数据", 'warning');
                return;
            }
            $.messager.confirm('确认对话框', '您确定执行该操作吗？', function (r) {
                if (r) {
                    $.get("/menu/changeState.do", {id: row.id}, function (data) {
                        if (data.success) {
                            $.messager.alert('温馨提示', '操作成功', 'info')
                            menu_datagrid.datagrid('reload');
                        } else {
                            $.messager.alert('温馨提示', data.msg, 'warning');
                        }
                    })
                }
            });
        },
        del: function () {
            var row = menu_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', "请选中一条数据", 'warning');
                return;
            }
            $.messager.confirm('确认对话框', '您确定要删除该菜单吗？', function (r) {
                if (r) {
                    $.get("/menu/delete.do", {id: row.id}, function (data) {
                        if (data.success) {
                            $.messager.alert('温馨提示', '删除成功', 'info')
                            menu_datagrid.datagrid('reload');
                        } else {
                            $.messager.alert('温馨提示', data.msg, 'warning');
                        }
                    })
                }
            });
        },
        reload: function () {
            menu_datagrid.datagrid('reload');
        },

        save: function () {
            menu_form.form('submit', {
                url: '/menu/saveOrUpdate.do',
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert('温馨提示', '保存成功', 'info')
                        methodObj.cancel();
                        menu_datagrid.datagrid('reload');
                    } else {
                        $.messager.alert('温馨提示', data.msg, 'error');
                    }
                }
            })
        },

        cancel: function () {
            menu_dialog.dialog('close');
        }
    }
    $("a[data-cmd]").click(function () {
        var methodName = $(this).data('cmd');
        methodObj[methodName]();
    })
    $("#parent_combotree").combotree({
        formatter:function (node) {
            return node.name;
        },
        url:'/menu/getRootMenu.do',
        textField:'name'
    })
})



